package ch.rheintalconnection.sysventory.technicalServices.wmi;

import ch.rheintalconnection.sysventory.dto.OperatingSystemDto;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class OperatingSystemScan {
		
	public static final String QUERY = "SELECT * FROM Win32_OperatingSystem";
	
	private ActiveXComponent axWMI;
	
	
	public OperatingSystemScan(ActiveXComponent axWMI) {
		this.axWMI = axWMI;
	}
	
	public OperatingSystemDto scan(){
		
		OperatingSystemDto operatingSystemDto = null;
		
		Variant vCollection = axWMI.invoke("ExecQuery", new Variant(QUERY));
		EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
		Dispatch item = null;
		
		while (enumVariant.hasMoreElements()) {
			item = enumVariant.nextElement().toDispatch();
			
			String description = Dispatch.call(item, "Caption").toString();
			String version  = Dispatch.call(item, "Version").toString();
			String technology = Dispatch.call(item, "OSArchitecture").toString();
			
			operatingSystemDto = new OperatingSystemDto(description, version, technology);
			return operatingSystemDto;
		}
		
		return null;
		
	}
}
